package com.elvin.zoe.pojo.structure;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.elvin.zoe.pojo.Message;

public class MessageOrganizer {

    private String groupDateformat;
    private final Map<Date, MessageGroup> mapDate2MessageGroup = new HashMap<Date, MessageGroup>();;
    private DatetimeRefiner datetimeRefiner;

    public MessageOrganizer(String groupDateformat) {
        super();
        this.groupDateformat = groupDateformat;
    }

    public void addAllMessage(Collection<Message> messages){
        for(Message message : messages)
            try {
                add(message);
            } catch (ParseException e) {
                e.printStackTrace();
            }
    }

    public void add(Message message) throws ParseException{
        SimpleDateFormat dateFormat = new SimpleDateFormat(groupDateformat);

        Date messageDatetime = message.getDateTime();
        if(datetimeRefiner != null)
            messageDatetime = datetimeRefiner.refine(messageDatetime);

        messageDatetime = dateFormat.parse(dateFormat.format(messageDatetime));
        MessageGroup messageGroup = mapDate2MessageGroup.get(messageDatetime);
        if(messageGroup == null){
            messageGroup = new MessageGroup(messageDatetime);
            mapDate2MessageGroup.put(messageDatetime, messageGroup);
        }
        messageGroup.add(message);
    }

    public void addAllMessageGroup(Collection<MessageGroup> messageGroups){
        for(MessageGroup messageGroup : messageGroups)
            try {
                add(messageGroup);
            } catch (ParseException e) {
                e.printStackTrace();
            }
    }

    public void add(MessageGroup messageGroup) throws ParseException{
        SimpleDateFormat dateFormat = new SimpleDateFormat(groupDateformat);

        Date messageDatetime = messageGroup.getMessageGroupDate();
        if(datetimeRefiner != null)
            messageDatetime = datetimeRefiner.refine(messageDatetime);

        messageDatetime = dateFormat.parse(dateFormat.format(messageDatetime));
        MessageGroup wrapMessageGroup = mapDate2MessageGroup.get(messageDatetime);
        if(wrapMessageGroup == null){
            wrapMessageGroup = new MessageGroup(messageDatetime);
            mapDate2MessageGroup.put(messageDatetime, wrapMessageGroup);
        }
        wrapMessageGroup.add(messageGroup);
    }

    public Collection<MessageGroup> getMessageGroup(){
        return mapDate2MessageGroup.values();
    }

    public String getGroupDateformat() {
        return groupDateformat;
    }
    public void setGroupDateformat(final String groupDateformat) {
        this.groupDateformat = groupDateformat;
    }

    public DatetimeRefiner getDatetimeRefiner() {
        return datetimeRefiner;
    }
    public void setDatetimeRefiner(DatetimeRefiner datetimeRefiner) {
        this.datetimeRefiner = datetimeRefiner;
    }
}

